<?php
namespace app\admin\controller;
use think\Controller;

use app\admin\model\Businesscenter as BusinesscenterModel;
use think\Session;

class Businesscenter extends AdminBase{
	//用户列表页面
	public function index(){
		

        $Businesscenter = new BusinesscenterModel();
        
        //$data = $payList->where('userid', $UserID)->select();
        $data = $Businesscenter->paginate(10);
         //dump($data);die;
        
        $this->assign('data',$data);
        
         $page = $data->render();
         $total = $data->total();
         $count = $data->count();

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('total',$total);
        $this->assign('count',$count);
        return $this->fetch();
	}
	//删除数据函数 ajax
    public function deleteBusiness(){

                $AllData = input('post.');
                //$AllData = '3,hao,2018-03-29 17:01:27,3333333,0,11111111,0';

        $User = new BusinesscenterModel();
       
        //从$data中得到名字字符串，并且转成数组
         $UserDeleteList = explode(',', $AllData['userinfo_str']);
         //dump($UserNameList);
         
         $SuccessArray = [];
         $FailArray = [];

        foreach ($UserDeleteList as $key => $value) {
                if($User->where(['id' => $value])->find()){
                        //从数据库能找到,就删除该条数据
                        if($User->where(['id' => $value])->delete()){

                                $SuccessArray[]= $value;
                                // echo "删除成功";
                        }else{
                                $FailArray[]= $value;
                                // echo "删除失败";
                        }
                }else{
                        $FailArray[]= $value;
                        //echo "没有本条数据";
                }
        }

        //返回的数组
        $StatusArray = [];
        $StatusArray['status'] = "1";
        $StatusArray['success'] = $SuccessArray ? $SuccessArray : NULL;
        //$StatusArray['success'] = ['aa','bb'];
        $StatusArray['fail'] = $FailArray ? $FailArray : NULL;

        //var_dump($StatusArray);die;
        return json_encode($StatusArray);
                
        }
        //修改数据函数 ajax
    public function modifyBusiness(){ 

        $AllData = input('post.');
       

        //$AllData = '2,hao,2018-03-29 17:01:27,3333333,0,11111111,0';

        // $AllData['userinfo_str']
        //$a = $a['action'];
        //$a = $a['username'];
        //$c = "action =".$a.",username = ".$b;
        //file_put_contents("cc.txt",$AllData['userinfo_str'],FILE_APPEND);
        //echo $a;
        $User = new BusinesscenterModel();
        $UserModifyList = explode(',', $AllData['userinfo_str']);
        //dump($UserModifyList);die;
        
        $SuccessArray = [];
        $FailArray = [];

        //看看存在吗
        if($User->where(['id' => $UserModifyList['0']])->find()){
                //存在就更新
                $Res = $User->save(
                        [
                    'username'  => $UserModifyList['1'],
                    'create_time'  => $UserModifyList['2'],
                    'all_pay' => $UserModifyList['3'],
                    'Business_level' => $UserModifyList['4'],
                    'Business_percent' => $UserModifyList['5']],
                                    
                                ['id' => $UserModifyList['0']]);
                        //给出成功标志
                        $SuccessArray[] = 1;
                if($Res){
                    // echo "111";
                }else{
                    // echo '222';
                }
        }else{
                //不存在直接返回
                $FailArray []= 1;
            // echo  'false';
        }die;
        //echo $result['id'];die;
        //返回的数组
        $StatusArray = [];
        $StatusArray['status'] = "1";
        $StatusArray['success'] = $SuccessArray ? $SuccessArray : NULL;
        //$StatusArray['success'] = ['aa','bb'];
        $StatusArray['fail'] = $FailArray ? $FailArray : NULL;

        // dump($StatusArray);die;
        //var_dump($StatusArray);die;
        return json_encode($StatusArray);
    }

}